package sz.szsmk.citizencard.ui.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.databinding.TransLogViewBinding;
import sz.szsmk.citizencard.model.network.bean.response.TransLogRes;
import sz.szsmk.citizencard.ui.activity.TransLogActivity;
import sz.szsmk.citizencard.util.StringUtils;

/**
 * Copyright (c)2018 author: zdh created on: 2018/7/18 20:50 description:
 */
public class TransLogAdapter extends BaseRecyclerAdapter<TransLogAdapter.TransLogHolder> {

    private TransLogActivity mContext;
    private List<TransLogRes.TradeDetailsBean> mData = new ArrayList<>();

    private static final int EMPTY_LAYOUT_TYPE = 1;
    private static final int DATA_LAYOUT_TYPE = 2;

    public TransLogAdapter(TransLogActivity transLogActivity) {
        mContext = transLogActivity;
    }

    @Override
    public TransLogHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return TransLogHolder.creat(parent);
//        if (viewType == EMPTY_LAYOUT_TYPE) {
//            View emptyView = LayoutInflater.from(mContext)
//                    .inflate(R.layout.recycle_empty_layout, parent, false);
//            return new EmptyHolder(emptyView);
//        } else {
//            return TransLogHolder.creat(parent);
//        }
    }

    @Override
    public void onBindViewHolder(TransLogHolder holder, int position) {
        if (holder instanceof TransLogHolder) {
            loadItemView(position, ((TransLogHolder) holder).binding);
        }
    }

//    @Override
//    public int getItemViewType(int position) {
//
//        return R.layout.trans_log_view;
////        if (mData.isEmpty()) {
////            return EMPTY_LAYOUT_TYPE;
////        } else {
////            return DATA_LAYOUT_TYPE;
////        }
//
//    }

    private void loadItemView(int position, TransLogViewBinding binding) {
        if (mData!=null && mData.size()>0){

            TransLogRes.TradeDetailsBean bean = mData.get(position);
            binding.logtimeTv.setText(bean.getTrade_date());
            binding.logtypeTv.setText(bean.getTrade_type());
            binding.logtypeTv2.setText(bean.getAcct_type());
            double balance = (double) bean.getTrade_money();
            double mBalance = balance / 100;
            String toString = StringUtils.doubleToString(mBalance);
//        final String string = doubleToString(mBalance);
            if (bean.getTrade_type().equals("消费")) {
                binding.logmoneyBalance.setText("-" + toString + "元");
            } else if (bean.getTrade_type().equals("充值")) {
                binding.logmoneyBalance.setText("+" + toString + "元");
            }else{
                binding.logmoneyBalance.setText(toString + "元");
            }
            binding.transBusness.setText(bean.getTrade_merchant());
        }
    }

    @Override
    public int getItemCount() {
//        return mData.isEmpty() ? 1 : mData.size();
        return null == mData ? 0 : mData.size();
    }

    public void setItemData(List<TransLogRes.TradeDetailsBean> details) {
        mData = details;
    }


    public static class TransLogHolder extends BaseHolder {

        private TransLogViewBinding binding;

        public static TransLogHolder creat(ViewGroup parent) {
            TransLogViewBinding binding = TransLogViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new TransLogHolder(binding);
        }

        public TransLogHolder(TransLogViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
